package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Servidor.Clases.EstadoTren;
import Servidor.Clases.GPS;
import Servidor.Clases.Vehiculo;

/**
 * Validados por Julen(08/12/11):
 * insert, delete, update, selectAll, selectOne
 *
 */
public class JDBCvehiculos implements JDBC {

	private static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";
	private static final String CONNECTION_URL = "jdbc:sqlite:resources/BBDD/trenes.s3db";
	//private static final String CONNECTION_URL = "jdbc:sqlite:C:/Users/Julen/Desktop/trenes.s3db";
	private Connection connection;
	private ResultSet resulset;
	
	public JDBCvehiculos(){
		try {
			Class.forName(DRIVER_CLASS_NAME);
			connection= DriverManager.getConnection(CONNECTION_URL);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void close() throws SQLException{
		connection.close();
	}
	
	public int insert(Object o) throws SQLException, ClassNotFoundException {
		if(o instanceof Vehiculo){
			Statement statement = null;
			statement =  connection.createStatement();
			String sql="";
			Vehiculo v=(Vehiculo)o;
			String gps=v.getGps().isEncendido() ? "1":"0";
			sql= "INSERT INTO vehiculo VALUES('"+v.getIdTren()+"','"+v.getEstadoTren()+"','"+gps+"');";
			int check=statement.executeUpdate(sql);
			statement.close();
			return check;
		}else
			throw new IllegalArgumentException("El parametro recibido deve ser de tipo Vehiculo!");
		}

	@Override
	public int delete(Object o) throws SQLException {
		if(o instanceof Vehiculo){
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		Vehiculo v=(Vehiculo)o;
		sql= "DELETE FROM vehiculo WHERE idTren=('"+v.getIdTren()+"');";
		int check=statement.executeUpdate(sql);
		statement.close();
		return check;
		}else
			throw new IllegalArgumentException("El parametro recibido deve ser de tipo Vehiculo!");
	}

	@Override
	public int update(Object o) throws SQLException, ClassNotFoundException {//cambiar
		int check=delete(o);
		if(check!=0){
			insert(o);
		}
		return check;
	}

	@Override
	public ArrayList<Vehiculo> selectAll() throws SQLException {
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		sql= "Select * FROM vehiculo;";
		resulset=statement.executeQuery(sql);
		ArrayList<Vehiculo> lista= new ArrayList<Vehiculo>();
		while(resulset.next()!=false){
			lista.add(convertVehiculo(resulset));
		}
		statement.close();
		return lista;
	}

	@Override
	public Vehiculo selectOne(Object o) throws SQLException {
		if(o instanceof Integer){
			Integer primaryKey= (Integer)o;
			Vehiculo vehiculo= null;
			Statement statement = null;
			statement =  connection.createStatement();
			String sql="";
			sql= "Select * FROM vehiculo WHERE idTren="+primaryKey+";";
			resulset=statement.executeQuery(sql);
			if(resulset.next())
				vehiculo= convertVehiculo(resulset);
			statement.close();
			return vehiculo;
		}else
			throw new IllegalArgumentException("El parametro recibido deve ser de tipo Integer!");
	}

	private Vehiculo convertVehiculo(ResultSet res) throws SQLException{
		Vehiculo vehiculo=new Vehiculo();
		vehiculo.setIdTren(Integer.parseInt(res.getString("IdTren")));
		String a=res.getString("estadoTren");
		vehiculo.setEstadoTren(EstadoTren.valueOf(a));
		boolean bool = res.getBoolean("gpsOn");
		GPS gps =new GPS(bool);
		vehiculo.setGps(gps);
		return vehiculo;
	}
	
}
